﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.Interop;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.ApplicationServices;

namespace WlanDesignDll
{
    public partial class DrawingRepairForm : Form
    {
        public DrawingRepairForm()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string threshold = txtThreshold.Text;
            if (threshold == "")
            {
                MessageBox.Show("阈值不能为空，请输入阈值！");
                return;
            }
            AcadApplication acAppComObj = null;
            const string strProgId = "AutoCAD.Application.18";
            // Get a running instance of AutoCAD
            try
            {
                acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
            }
            catch // An error occurs if no instance is running
            {
                try
                {
                    // Create a new instance of AutoCAD
                    acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);

                }
                catch
                {
                    // If an instance of AutoCAD is not created then message and exit
                    System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" + " could not be created.");
                    return;
                }
            }
            Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            string[] name = acDoc.Name.Split('.');
            string iniName = name[0] + ".ini";
            WlanDesign.FormStartup.WriteIniTxt(iniName, "parameter", "threshold", threshold);
            AcadDocument acDocComObj;
            acDocComObj = acAppComObj.ActiveDocument;
            acDocComObj.SendCommand("DrawingRepair ");
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
